<template>
  <div>
    <!-- 已派单列表 2018/6/23 zhangQ -->
    <Table ref="orderTab" :columns="orderColumn" border :data="orderData" :height="subOrderTabHeight" @on-expand="showExpand" class="subOrderDetail"></Table>
  </div>
</template>

<script>
  import moment from 'moment'
  import _ from 'lodash'
  import {getOwnFleet} from '@api/backEnd/station'
  import viewDetailSubOrder from './viewDetailSubOrder'
  import {queryDispatched, dispatchCancelToGroupFleets} from '@api/backEnd/mainOrder'
  import {mapGetters} from 'vuex'
  // import {getFleetOrderDetailList, dispatchCancelToGroupV3} from '@api/backEnd/mainOrder'
  export default {
    name: 'dispatchedList',
    data () {
      let orderColumn = [
        {
          type: 'expand',
          width: 50,
          render: (h, params) => {
            if (params.row.groupNo) {
              return h(viewDetailSubOrder, {
                props: {
                  currentSubOrderList: params.row.subList
                }
              })
            } else {
              return null
            }
          }
        },
        {
          type: 'index',
          width: 60
        },
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '派单车队',
          width: 150,
          sortable: true,
          render: (h, params) => {
            let thisdata = params.row.fleetNames
            thisdata = thisdata ? thisdata : ''
            return (<span>{thisdata}</span>)
          }
        },
        {
          title: '派单时间',
          width: 150,
          sortable: true,
          render: (h, params) => {
            let thisdata = params.row.createTime
            thisdata = thisdata ? moment(thisdata).format('YYYY-MM-DD HH:mm') : ''
            return (<span>{thisdata}</span>)
          }
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          minWidth: 250,
          render: (h, params) => {
            // let sendNoticeBtn = (<i-button type="error" size="small" style="margin-right: 5px;" onClick={() => this.sendNoticeAction(params.row)}>预通知车队</i-button>)
            let viewDetailBtn = (<i-button type="primary" size="small" style="margin-right: 5px;" onClick={() => this.dispatchCancelToGroup(params.row)}>撤销派单</i-button>)
            // powerUtil.contains('sendNotice') && btnArray.push(sendNoticeBtn)
            return (viewDetailBtn)
          }
        }
      ]
      let orderData = []
      let trackName = ''
      let subOrderTabHeight = 550
      let fleets = []
      let datas = {orderColumn, orderData, trackName, subOrderTabHeight, fleets}
      return datas
    },
    props: {
      currentMainOrder: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    methods: {
      showExpand (row, status) {},
      dispatchCancelToGroup (row) {
        dispatchCancelToGroupFleets({groupNo: row.groupNo}).then(res => {
          if (+res.data.isSuccess === 1) {
            this.getData(this.fleets)
          } else {
            this.$Notice.warning({
              title: '提示',
              desc: '出现错误'
            })
          }
        })
      },
      getData (fleets) {
        queryDispatched({mainOrderNo: this.currentMainOrder.mainOrderNo}).then(res => {
          if (res.data.isSuccess === 1) {
            let allData = res.data.data
            console.log(this.currentUser.userId)
            let resetData = []
            _.forEach(allData, (item) => {
              if (item.orgId !== this.currentUser.userId) {
                resetData.push(item)
              }
            })
            console.log(resetData)
            let resData = []
            _.forIn(_.groupBy(resetData, 'dispatchGroupNo'), (item, key) => {
              let fleetNames = []
              item.forEach(obj => {
                fleetNames.push(obj.orgName)
              })
              resData.push({
                groupNo: key,
                fleetNames: fleetNames.join(','),
                createTime: item[0].createTime,
                subList: item[0].xlFleetOrderDetails
              })
            })
            this.orderData = resData
          } else {
            this.$Notice.warning({
              title: '提示',
              desc: '出现错误'
            })
          }
        })
      }
    },
    created () {
    },
    mounted () {
      getOwnFleet().then(res => {
        if (res.data.isSuccess === 1) {
          let fleets = res.data.data
          this.fleets = fleets
          // this.newObj = this.trackName
          this.getData(fleets)
        }
      })
    },
    components: {viewDetailSubOrder},
    computed: {
      ...mapGetters(['currentUser'])
    }
  }
</script>
<style>
  .subOrderDetail {
    overflow: auto;
  }

  .subOrderDetail .ivu-table {
    overflow: auto;
  }
</style>
